package com.fingard.dsp.bank.directbank.abc23.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 摘要算法帮助类 
 * @author Administrator
 *
 */
public class DigestHelper {
	/**
	 * 判断两个文件的 MD5 摘要是否相同
	 * @param fileName1
	 * @param fileName2
	 * @return
	 * @throws NoSuchAlgorithmException 
	 */
	public static boolean digestEqual(String fileName1, String fileName2) throws IOException, NoSuchAlgorithmException 
	{
		byte[] bytes1 = IOHelper.fileToBytes(fileName1);
		byte[] bytes2 = IOHelper.fileToBytes(fileName2);
		String md5Hex1 = getMD5HexString(bytes1);
		String md5Hex2 = getMD5HexString(bytes2);
		return md5Hex1.equals(md5Hex2);
	}

	/**
	 * 对源数据进行 MD5 摘要
	 * @param sourceBytes
	 * @return
	 * @throws NoSuchAlgorithmException 
	 */
	public static byte[] getMD5(byte[] sourceBytes) throws NoSuchAlgorithmException
	{
		MessageDigest md = MessageDigest.getInstance("MD5"); 
		md.update(sourceBytes); 
		return md.digest(); 
	}
	/**
	 * 对源数据进行 MD5 摘要，返回 16进制表示方式字符串
	 * @param sourceBytes
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static String getMD5HexString(byte[] sourceBytes) throws NoSuchAlgorithmException
	{
		return StringHelper.bytesToHexString(getMD5(sourceBytes));
	}
	/**
	 * 对源数据进行 SHA1 摘要
	 * @param sourceBytes
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] getSHA1(byte[] sourceBytes) throws NoSuchAlgorithmException
	{
		MessageDigest md = MessageDigest.getInstance("SHA"); 
		md.update(sourceBytes); 
		return md.digest(); 
	}
	/**
	 * 对源数据进行 SHA1 摘要，返回 16进制表示方式字符串
	 * @param sourceBytes
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static String getSHA1HexString(byte[] sourceBytes) throws NoSuchAlgorithmException
	{
		return StringHelper.bytesToHexString(getSHA1(sourceBytes));
	}
}
